package com.atguigu.ssyx.acl.controller;


import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/admin/acl/role")
@Api(tags = "角色接口")
public class RoleController {
    @Autowired
    private RoleService roleService;


    /**
     * @param page  第几页
     * @param limit 一页多少个
     * @return
     */
    @GetMapping("{page}/{limit}")
    @ApiOperation("获取角色分页列表")
    public Result getPageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "一页包含数量", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "roleQueryVo", value = "请求的角色名字", required = false)
            RoleQueryVo roleQueryVo) {

//        第page页,一页有limit个.
        Page<Role> pageParam = new Page<>(page, limit);
        IPage<Role> pageModel = roleService.selectRolePage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }


    @ApiOperation("获取角色信息")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id) {

        Role byId = roleService.getById(id);

        return Result.ok(byId);
    }

    @PostMapping("save")
    @ApiOperation("新增角色")
    public Result save(@RequestBody Role role) {
        roleService.save(role);
        return Result.ok(null);
    }

//    更新数据
    @PutMapping("update")
    @ApiOperation("修改数据")
    public Result updateById(@RequestBody Role role) {
        boolean is_success = roleService.updateById(role);
        if (!is_success) {
            return Result.fail(null);
        }else{
            return Result.ok(null);
        }
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation("删除数据")
    public Result remove(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.ok(null);
    }

    @DeleteMapping("batchRemove")
    @ApiOperation("批量删除数据")
    public Result removeRoles(@RequestBody List<Long> ids) {
        boolean b = roleService.removeByIds(ids);
        return Result.ok(null);
    }

}
